package com.qf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qf.aop.annocation.LoginUser;
import com.qf.common.ResultEntity;
import com.qf.entity.Address;
import com.qf.entity.User;
import com.qf.service.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressController")
@Slf4j
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @RequestMapping("/getAddressById/{id}")
    public Address getAddressById(@PathVariable Integer id){
        return addressService.selectById(id);
    }

    @GetMapping("/getAddressListByUid/{uid}")
    public List<Address> getAddressListByUid(@PathVariable Integer uid) {
        return addressService.selectList(new EntityWrapper<Address>().eq("uid", uid));
    }

    @RequestMapping("/insert")
    @LoginUser
    public ResultEntity insert(User user, @RequestBody Address address) {
        log.debug("{}", address);
        if (user == null) {
            return ResultEntity.error("你还没有登录，请先登录");
        }
        address.setUid(user.getId());
        address.setDef(address.getDef() ==null?0:address.getDef());
        addressService.addAddress(address);
        return ResultEntity.success();
    }
}
